#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<unordered_map>
using namespace std;

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        unordered_map<char, int> hash1;
        unordered_map<char, int> hash2;

        vector<int> ret;
        for (int i = 0; i < p.size(); i++)
            hash2[p[i]]++;
        int left = 0, right = 0;
        for (; right < s.size(); right++)
        {
            hash1[s[right]]++;
            while (right - left + 1 >= p.size())
            {
                if (hash1 == hash2) ret.push_back(left);

                hash1[s[left]]--;
                if (hash1[s[left]] == 0) hash1.erase(s[left]);
                left++;
            }
        }
        return ret;
    }
};